import { useRef, useEffect } from 'react'
import { number } from 'prop-types'
export const toClassNames = (params: { [key: string]: boolean }) => {
	let classNames = []
	for (let key in params) {
		if (params[key]) classNames.push(params[key])
	}
	return classNames.join(' ')
}
interface Tree {
	id: number
	parentId: number
	children?: Tree[]
}
export function toTreeData(data: Tree[]) {
	let cache = new Map<number, Tree>()
	let treeData = []
	for (let item of data) {
		item.children = []
		let parentItem = cache.get(item.parentId)
		if (parentItem) {
			parentItem.children.push(item)
		} else {
			treeData.push(item)
		}
		cache.set(item.id, item)
	}
	return treeData
}

export function usePrevious(value: any) {
	const ref = useRef()
	useEffect(() => {
		ref.current = value
	})
	return ref.current
}

export function shallowEqual(a: any, b: any) {
	if (a === b) return true
	let keysA = Object.keys(a)
	let keysB = Object.keys(b)
	if (keysA.length !== keysB.length) return false
	let hasDiff = keysA.some(key => a[key] !== b[key])
	return !hasDiff
}

export function formatMoney(value: number | string) {
	var parts = number.toString().split('.')
	parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
	return parts.join('.')
}
export function getMoneyFen(arg1: number) {
	let m = 0
	let s1 = arg1.toString()
	let data = s1.split(".")
	if (data.length < 2) {
		return Number(data[0] + '00')
	} else {
		return Number(data[0] + (data[1] + '00').substring(0, 2))
	}
}
